﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CustomTextbox
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:WatermarkTextbox1 runat=server></{0}:WatermarkTextbox1>")]
    public class WatermarkTextbox : TextBox
    {
        #region Private Members
        private RequiredFieldValidator _requiredFieldValidatorEmptyString;
        private RequiredFieldValidator _requiredFieldValidatorInitialValue;
        private string _clientCssClass = string.Empty;
        #endregion

        #region Public Properties
        public string WatermarkText
        {
            get
            {
                return this.ToolTip;
            }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    this.Text = value;
                    this.ToolTip = value;
                }
            }
        }
        public string ClientCssClass
        {
            get
            {
                return _clientCssClass;
            }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    _clientCssClass = value;
                    this.CssClass = _clientCssClass + " " + "watermark";
                }
            }
        }
        public bool RequiredFieldValidatorEnabled { get; set; }
        #endregion

        #region Constructor
        public WatermarkTextbox()
        {
            _requiredFieldValidatorEmptyString = new RequiredFieldValidator();
            _requiredFieldValidatorInitialValue = new RequiredFieldValidator();
        }
        #endregion

        protected override void OnInit(EventArgs e)
        {
            if (RequiredFieldValidatorEnabled)
            {
                //_requiredFieldValidatorEmptyString = new RequiredFieldValidator();
                _requiredFieldValidatorEmptyString.ID = this.ID + "RequiredValidatorEmptyString";
                _requiredFieldValidatorEmptyString.ControlToValidate = this.ID;
                _requiredFieldValidatorEmptyString.Display = ValidatorDisplay.None;
                _requiredFieldValidatorEmptyString.EnableClientScript = true;
                base.Controls.Add(_requiredFieldValidatorEmptyString);

                //_requiredFieldValidatorInitialValue = new RequiredFieldValidator();
                _requiredFieldValidatorInitialValue.ID = this.ID + "RequiredValidatorInitialValue";
                _requiredFieldValidatorInitialValue.ControlToValidate = this.ID;
                _requiredFieldValidatorInitialValue.Display = ValidatorDisplay.None;
                _requiredFieldValidatorInitialValue.InitialValue = this.WatermarkText;
                _requiredFieldValidatorInitialValue.EnableClientScript = true;
                base.Controls.Add(_requiredFieldValidatorInitialValue);
            }
            base.OnInit(e);
        }
        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);
            if (RequiredFieldValidatorEnabled)
            {
                _requiredFieldValidatorEmptyString.RenderControl(writer);
                _requiredFieldValidatorInitialValue.RenderControl(writer);
            }
        }
    }
}
